require([
    "esri/Color",
    "esri/map",
    "esri/layers/ArcGISDynamicMapServiceLayer",
    "esri/layers/ArcGISTiledMapServiceLayer",
    "esri/layers/FeatureLayer", "esri/layers/GraphicsLayer",
    "dojo/parser",
    "dojo/domReady!"
], function (Color, Map, ArcGISDynamicMapServiceLayer, ArcGISTiledMapServiceLayer,
    FeatureLayer, GraphicsLayer, parser) {
    parser.parse();

    ///设置初始范围
    var initExtent = new esri.geometry.Extent();
    initExtent.xmin = -96772;
    initExtent.xmax = 70772;
    initExtent.ymin = -28592;
    initExtent.ymax = 30000;
    initExtent.setSpatialReference(shanghaiSpatialRef);

    var previewMap = new Map("preview", {
        extent: initExtent,
        logo: false,
        showAttribution: false
    });

    var base2 = new ArcGISTiledMapServiceLayer(baseMapUrl, {
        "visible": true
    });
    base2.setScaleRange(500000, 500);

    previewMap.addLayer(base2);


    var previewLayer = "";
    $("#previewBtn").click(function () {
        previewMap.removeLayer(previewLayer);
        var url = $("#previewUrl").val();
        if (url != "" && url.endWith('MapServer')) {
            previewLayer = new ArcGISDynamicMapServiceLayer(url, {
                spatialReference: shanghaiSpatialRef,
                opacity: 0.8
            });

            previewLayer.on("load", function (e) {
                //打开第一个图层进行预览
                layerInfos = previewLayer.layerInfos;

                let count = 0;
                for (let i = 0; i < layerInfos.length; i++) {
                    if (layerInfos[i].subLayerIds == null) {
                        count++;
                        if (count == 3) {
                            previewLayer.setVisibility(true);
                            previewLayer.setVisibleLayers([layerInfos[i].id]);
                            break;
                        }
                    }
                }
                // previewLayer.setVisibility(true);
                // previewLayer.setVisibleLayers([0]);
                previewMap.addLayer(previewLayer);
                // 防止图层的初始范围过大导致无法显示
                const extent = previewLayer.initialExtent
                if (extent.ymin > -150000 && extent.xmin > -150000 && extent.xmax < 150000 && extent.ymax < 150000) {
                    previewMap.setExtent(previewLayer.initialExtent);
                }
            })


        } else {
            warningAlert("请输入正确的地图服务地址");
        }

    });

    var addServiceLayerIds = [];

    $("#addServiceModal .okBtn").click(function () {
        var url = $("#previewUrl").val();

        if (url != "" && url.endWith('MapServer')) {
            addServiceLayerIds.push("addServiceLayer" + addServiceLayerIds.length);

            let addServiceLayerId = addServiceLayerIds[addServiceLayerIds.length - 1]

            var listDiv = '<div id="' + addServiceLayerId + '" class="ztree scrollbar3"></div>';
            $("#con_mainio_4 .listPart").append(listDiv);

            var addServiceConfig = {
                layerId: addServiceLayerId,
                visible: false,
                opacity: 0.6,
                url: url,
                zTreeDivId: addServiceLayerId,
                zTreeSearchInputId: "otherKey",
                isZoom: true
            }

            initDynamicLayer(addServiceConfig);

            $("#addServiceModal").modal("hide");
            previewMap.removeLayer(previewLayer);
            $("#previewUrl").val("");
            $("#mainio4").click();
        } else {
            warningAlert("请输入正确的服务地址");
        }

    });

    $("#addServiceModal").on('hidden.bs.modal', function () {
        previewMap.removeLayer(previewLayer);
        $("#previewUrl").val("");
    })

});